package cc.geguol.iodemo.streamSlipt;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.Vector;

/**
 * 作者：13687
 * 日期：2017/6/17
 * 项目：IODemo
 * 描述：合并多个文件
 */
public class SequenceInputStreamDemo {
	public static void main(String[] args) throws IOException {
		mergetext();
//		splitePng();
//		mergePng();
	}

	private static void mergetext() throws IOException {
		Vector<InputStream> streamVector = new Vector<>();
		FileInputStream fileInputStream1 = new FileInputStream("./files/text1.txt");
		FileInputStream fileInputStream2 = new FileInputStream("./files/text2.txt");
		FileInputStream fileInputStream3 = new FileInputStream("./files/text3.txt");

		streamVector.add(fileInputStream1);
		streamVector.add(fileInputStream2);
		streamVector.add(fileInputStream3);

		SequenceInputStream sequenceInputStream = new SequenceInputStream(streamVector.elements());
		BufferedInputStream bufferedInputStream = new BufferedInputStream(sequenceInputStream);
		BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("./files/text.txt"));

		int ch = 0;

		while ((ch=bufferedInputStream.read())!=-1){
			bufferedOutputStream.write(ch);
		}

		bufferedInputStream.close();
		bufferedOutputStream.close();
	}

	private static void mergePng() throws IOException {
		Vector<InputStream> streamVector = new Vector<>();
		FileInputStream fileInputStream1 = new FileInputStream("./files/1.temp");
		FileInputStream fileInputStream2 = new FileInputStream("./files/2.temp");
		FileInputStream fileInputStream3 = new FileInputStream("./files/3.temp");

		streamVector.add(fileInputStream1);
		streamVector.add(fileInputStream2);
		streamVector.add(fileInputStream3);

		SequenceInputStream sequenceInputStream = new SequenceInputStream(streamVector.elements());
		BufferedInputStream bufferedInputStream = new BufferedInputStream(sequenceInputStream);
		BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("./files/merge.png"));

		int ch = 0;

		while ((ch=bufferedInputStream.read())!=-1){
			bufferedOutputStream.write(ch);
		}

		bufferedInputStream.close();
		bufferedOutputStream.close();
	}

	// 将图片分割成多分
	public static void splitePng() throws IOException {
		FileInputStream fileInputStream = new FileInputStream("./img/iofamily.png");
		byte[] buff= new byte[1024*30];//按照1M分割
		int count = 1;
		while (fileInputStream.read(buff)!=-1){
			FileOutputStream fileOutputStream = new FileOutputStream("./files/"+(count++)+".temp");
			fileOutputStream.write(buff,0,buff.length);
			fileOutputStream.close();
		}
		fileInputStream.close();

	}
}
